﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Util.Extension;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    [Area("Console")]
    public class ReportsController : BaseController
    {
        private readonly IWebHostEnvironment _hostingEnv;
        private readonly mtContext _context;
        public ReportsController(mtContext context, IWebHostEnvironment hostingEnv) : base(context, hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
        }

        #region 入金报表
        public IActionResult CashDepositReport()
        {
            return View();
        }

        /// <summary>
        /// 获取入金报表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetCashDepositReport(int page, int limit, int crmId = 0)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.CrmDeposits
                           select new
                           {
                               id = y.Id,
                               crmId = y.CrmId,
                               login = y.Login,
                               mothod = y.Mothod,
                               fromAmount = y.FromAmount,
                               fromCurrency = y.FromAmountCurrency,
                               toAmount = y.ToAmount,
                               toCurrency = y.ToAmountCurrency,
                               exchangeRate = y.ExchangeRate,
                               depositAmount = y.DepositAmount,
                               depositCurrency = y.DepositAmountCurrency,
                               state = y.State,
                               fees = y.Fees,
                               createDate = y.CreateDate
                           };
                if (crmId != 0)
                {
                    data = data.Where(k => k.crmId == crmId);
                }
                res.Code = 0;
                res.Count = data.Count();
                res.Data = await data.Skip((page - 1) * limit).Take(limit).OrderBy(k => k.id).ToListAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 挂单交易
        public IActionResult TransactionReport()
        {
            return View();
        }
        #endregion

        #region 持仓订单
        public IActionResult PositionReport()
        {
            return View();
        }
        #endregion

        #region 交易历史
        public IActionResult OrderHistory()
        {
            return View();
        }
        #endregion

        #region CRM组报表
        public IActionResult CrmGroupReport()
        {
            return View();
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetCrmGroupListJson(int page, int limit, int? groupId = null)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.Crmgroupreportviews
                           select new
                           {
                               groupId = y.GroupId,
                               groupName = string.IsNullOrEmpty(y.GroupName) ? "未分组用户" : y.GroupName,
                               dealCount = y.DealCount,
                               volume = y.Volume == null ? "0" : y.Volume.ToString(),
                               profitAndLoss = y.Profit == null ? "0.00" : ((decimal)y.Profit).ToString("#,##0.00")
                           };

                if (groupId != null)
                {
                    data = data.Where(k => k.groupId == groupId);
                }
                data = data.OrderByDescending(k => k.dealCount);
                res.Code = 0;
                res.Count = data.Count();
                res.Data = await data.Skip((page - 1) * limit).Take(limit).ToListAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion
    }
}
